const app = getApp();
Page({
  data: {
    motion: [
      { name: '保龄球', value: '7', checked: false }
    ],
    food: [],
    book: [],
    movie: [],
    hobby: [],
    tourism: [],
    program: [],
    amuse: []
  },
  onLoad: function (options) {
    this.initPage();
  },
  initPage: function () {
    // app.detail = utilities.base64TojsonObj(sessionStorage.userInfo).detail;
    var _this = this;
    app.getUserInfoSyn({"openId":wx.getStorageSync('openid')}).then(function () {
      var userInfo = wx.getStorageSync('userInfo');
      app.doReq(app.globalData.DICTIONARY, {
        parentId: '773,806,821,843,860,881,895,908'
      }, 'get', function (res) {
        console.log(res);
        var data = res.data;
        if (data.code == 200) {
          var lists = data.obj
          var usermotions = userInfo.detail.motion;
          var userfoods = userInfo.detail.food;
          var userbooks = userInfo.detail.book;
          var usermovies = userInfo.detail.movie;
          var userhobbys = userInfo.detail.hobby;
          var usertourisms = userInfo.detail.tourism;
          var userprograms = userInfo.detail.program;
          var useramuses = userInfo.detail.amuse;

          var usermotionsT = []
          for (let i of lists[773]) {
            let obj = {};
            obj.name = i.name;
            obj.value = i.id;
            if (usermotions.indexOf(i.id) >= 0) {
              obj.checked = true;
            } else {
              obj.checked = false;
            }
            usermotionsT.push(obj);
          }

          var userfoodsT = []
          for (let i of lists[806]) {
            let obj = {};
            obj.name = i.name;
            obj.value = i.id;
            if (userfoods.indexOf(i.id) >= 0) {
              obj.checked = true;
            } else {
              obj.checked = false;
            }
            userfoodsT.push(obj);
          }

          var userbooksT = []
          for (let i of lists[821]) {
            let obj = {};
            obj.name = i.name;
            obj.value = i.id;
            if (userbooks.indexOf(i.id) >= 0) {
              obj.checked = true;
            } else {
              obj.checked = false;
            }
            userbooksT.push(obj);
          }

          var usermoviesT = []
          for (let i of lists[843]) {
            let obj = {};
            obj.name = i.name;
            obj.value = i.id;
            if (usermovies.indexOf(i.id) >= 0) {
              obj.checked = true;
            } else {
              obj.checked = false;
            }
            usermoviesT.push(obj);
          }

          var userhobbysT = []
          for (let i of lists[860]) {
            let obj = {};
            obj.name = i.name;
            obj.value = i.id;
            if (userhobbys.indexOf(i.id) >= 0) {
              obj.checked = true;
            } else {
              obj.checked = false;
            }
            userhobbysT.push(obj);
          }

          var usertourismsT = []
          for (let i of lists[881]) {
            let obj = {};
            obj.name = i.name;
            obj.value = i.id;
            if (usertourisms.indexOf(i.id) >= 0) {
              obj.checked = true;
            } else {
              obj.checked = false;
            }
            usertourismsT.push(obj);
          }

          var userprogramsT = []
          for (let i of lists[895]) {
            let obj = {};
            obj.name = i.name;
            obj.value = i.id;
            if (userprograms.indexOf(i.id) >= 0) {
              obj.checked = true;
            } else {
              obj.checked = false;
            }
            userprogramsT.push(obj);
          }

          var useramusesT = []
          for (let i of lists[908]) {
            let obj = {};
            obj.name = i.name;
            obj.value = i.id;
            if (useramuses.indexOf(i.id) >= 0) {
              obj.checked = true;
            } else {
              obj.checked = false;
            }
            useramusesT.push(obj);
          }
          _this.setData({
            motion: usermotionsT,
            food: userfoodsT,
            book: userbooksT,
            movie: usermoviesT,
            hobby: userhobbysT,
            tourism: usertourismsT,
            program: userprogramsT,
            amuse: useramusesT,
          })
        }
      });
    })
  },
  whenTap: function (e) {
    var _this = this;
    let value = e.currentTarget.dataset.value;
    let nodetype = e.currentTarget.dataset.type;
    switch (nodetype) {
      case 'motion':
        var motion = _this.data.motion
        for (let i of motion) {
          if (i.value == value) {
            if (i.checked) {
              i.checked = false
            } else {
              i.checked = true
            }
          }
        }
        _this.setData({
          motion: motion
        })
        break;
      case 'food':
        var food = _this.data.food
        for (let i of food) {
          if (i.value == value) {
            if (i.checked) {
              i.checked = false
            } else {
              i.checked = true
            }
          }
        }
        _this.setData({
          food: food
        })
        break;
      case 'book':
        var book = _this.data.book
        for (let i of book) {
          if (i.value == value) {
            if (i.checked) {
              i.checked = false
            } else {
              i.checked = true
            }
          }
        }
        _this.setData({
          book: book
        })
        break;
      case 'movie':
        var movie = _this.data.movie
        for (let i of movie) {
          if (i.value == value) {
            if (i.checked) {
              i.checked = false
            } else {
              i.checked = true
            }
          }
        }
        _this.setData({
          movie: movie
        })
        break;
      case 'hobby':
        var hobby = _this.data.hobby
        for (let i of hobby) {
          if (i.value == value) {
            if (i.checked) {
              i.checked = false
            } else {
              i.checked = true
            }
          }
        }
        _this.setData({
          hobby: hobby
        })
        break;
      case 'tourism':
        var tourism = _this.data.tourism
        for (let i of tourism) {
          if (i.value == value) {
            if (i.checked) {
              i.checked = false
            } else {
              i.checked = true
            }
          }
        }
        _this.setData({
          tourism: tourism
        })
        break;
      case 'program':
        var program = _this.data.program
        for (let i of program) {
          if (i.value == value) {
            if (i.checked) {
              i.checked = false
            } else {
              i.checked = true
            }
          }
        }
        _this.setData({
          program: program
        })
        break;
      case 'amuse':
        var amuse = _this.data.amuse
        for (let i of amuse) {
          if (i.value == value) {
            if (i.checked) {
              i.checked = false
            } else {
              i.checked = true
            }
          }
        }
        _this.setData({
          amuse: amuse
        })
        break;
    }
  },
  doSave: function () {
    let _this = this;
    let data = this.data;
    var req={
      id:app.globalData.userid
    };
    for (let i in data) {
          if(typeof(data[i])=='object' && data[i].length>0){
            let str='';
            for(let x in data[i]){
                  if(data[i][x].checked){
                    str += data[i][x].value + ",";
                  }
            }
            if(str!='' && str!=null){
              var newStr = str[str.length - 1] == ',' ? str.substring(0, str.length - 1) : str
              req['xgdetail.' + i] = newStr;
            }
          }
    }
    app.doReq(app.globalData.SERVICE.USERCENTER.HOBBY_SAVE, req,'get', function (res) {
      var data = res.data
      if (data.code == 200 && data.type == 'SUCCESS') {
        wx.showToast({
          title: '保存成功！',
          icon: 'success'
        });
        app.getUserInfoSyn({"openId":wx.getStorageSync('openid')}).then(function (value) {
          var pages = getCurrentPages();
          var currentPage = pages[pages.length - 1];//当前页面
          var pervPagge = pages[pages.length - 2];//上个页面
          pervPagge.setData({
            reloadFlag: true,
          })
          wx.navigateBack({});
        });
      }
    })
    // var labelId = [];
    // for (let i of _this.data.labels) {
    //   if (i.checked) {
    //     labelId.push(i.value);
    //   }
    // }
    // app.doReq(app.globalData.SERVICE.USERCENTER.TAG_SAVE, {
    //   'xgdetail.label': labelId.toString(),
    //   'id': app.globalData.userid
    // }, 'get', function (res) {
    //   var data = res.data
    //   if (data.code == 200 && data.type == 'SUCCESS') {
    //     wx.showToast({
    //       title: '保存成功！',
    //       icon: 'success'
    //     });
    //     app.getUserInfoSyn({"openId":wx.getStorageSync('openid')}).then(function (value) {
    //       var pages = getCurrentPages();
    //       var currentPage = pages[pages.length - 1];//当前页面
    //       var pervPagge = pages[pages.length - 2];//上个页面
    //       pervPagge.setData({
    //         reloadFlag: true,
    //       })
    //       wx.navigateBack({});
    //     });
    //   }
    // })
  },
  doCancel: function () {
    wx.navigateBack({})
  }
})